<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/26/026
 * Time: 17:12
 */

namespace app\api\controller;

use think\Controller;
use Workerman\Connection\ConnectionInterface;
use Workerman\Worker;

class WorkerServer extends  Controller
{
    // 创建一个Worker监听2346端口，使用websocket协议通讯
    public function index()
    {

        // 创建一个Worker监听2346端口，使用websocket协议通讯
        $ws_worker = new Worker("websocket://127.0.0.1:2346");

        // 启动4个进程对外提供服务
        $ws_worker->count = 4;

        // 当收到客户端发来的数据后返回hello $data给客户端
        $ws_worker->onMessage = function (ConnectionInterface $connection, $data) {
            // 向客户端发送hello $data
            $connection->send('Hello： ' . $data);
        };
        // 运行worker
        Worker::runAll();
    }
}